package com.lzs.esubscribe.controller;

import com.lzs.esubscribe.api.CommonResult;
import com.lzs.esubscribe.dto.*;
import com.lzs.esubscribe.entity.ESUser;
import com.lzs.esubscribe.service.IUrlService;
import com.lzs.esubscribe.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: lzs
 * @Date: 2022-03-17
 * @Description：
 */
@ResponseBody
@RestController
@RequestMapping("/url")
@Api(value = "资源")
public class UrlController {

    @Autowired
    IUrlService iUrlService;

    /**
     * @param
     * @return
     * @description 添加资源
     * @date 2022-03-17
     */
    @ApiOperation(value = "添加资源", notes = "添加资源", httpMethod = "POST",response = CommonResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/addUrl")
    public CommonResult addUrl(@RequestBody AddUrlDTO addUrlDTO) {
        int res = iUrlService.addUrl(addUrlDTO);
        if (res != 0) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    /**
     * @param
     * @return
     * @description 修改资源
     * @date 2022-03-17
     */
    @ApiOperation(value = "修改资源", notes = "修改资源", httpMethod = "POST",response = CommonResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/modifyUrl")
    public CommonResult modifyUrl(@RequestBody ModifyUrlDTO modifyUrlDTO) {
        int res = iUrlService.modifyUrl(modifyUrlDTO);
        if(res!=0){
            return CommonResult.success("修改成功！");
        }else {
            return CommonResult.failed("修改失败！");
        }
    }


    /**
     * @param
     * @return
     * @description 删除资源
     * @date 2022-03-17
     */
    @ApiOperation(value = "删除资源", notes = "删除资源", httpMethod = "POST",response = CommonResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/deleteUrl")
    public CommonResult deleteUrl(@RequestBody DeleteUrlDTO deleteUrlDTO) {
        int res = iUrlService.deleteUrl(deleteUrlDTO);
        if(res!=0){
            return CommonResult.success("删除成功！");
        }else {
            return CommonResult.failed("删除失败！");
        }
    }

    /**
     * @param
     * @return
     * @description 导入浏览器收藏
     * @date 2022-03-17
     */
    @ResponseBody
    @ApiOperation(value = "导入浏览器收藏", notes = "导入浏览器收藏", httpMethod = "POST",response = ImportResultDTO.class)
    @RequestMapping(method = RequestMethod.POST, value = "/importSubscribe")
    public CommonResult importSubscribe(ImportSubscribeDTO importSubscribeDTO) {
        if (importSubscribeDTO.getFile() == null || importSubscribeDTO.getFile().isEmpty()) {
            return CommonResult.failed("空文件");
        }
        ImportResultDTO res = iUrlService.importSubscribe(importSubscribeDTO);
        if(res!=null){
            return CommonResult.success(res.getResultList(),"导入成功,总导入项目："+Integer.valueOf(res.getSuccessNum()+res.getFailNum())+",成功："+res.getSuccessNum()+",失败:"+res.getFailNum());
        }else {
            return CommonResult.failed("用户不存在");
        }
    }
}
